<template>
	<view class="parent">
		<view class="login-title">{{loginTitle}}</view>

		<view class="login-input">
			<view class="label"> +86 </view>
			<input class="et" placeholder="请输入手机号" v-model="account" />
		</view>

		<view class="login-input" v-if="2==loginType">
			<view class="label">验证码</view>
			<input class="et" placeholder="请输入验证码" v-model="code" />
			<zy-button-code :account='account'></zy-button-code>
		</view>

		<view class="login-input" v-if="1==loginType">
			<view class="label">密码</view>
			<input class="et" placeholder="请输入密码" v-model="password" password />
		</view>

		<view class="register">
			<view class="textButton" @click="loginTypeChange">{{loginTypeTitle}}</view>
			<view class="textButton" @click="goRegister">注册</view>
		</view>

		<zy-button text="立即登录" @click.native="requestLogin" style="margin-top: 100rpx;"></zy-button>
		<zy-privacy ref="privacy"></zy-privacy>
	</view>
</template>

<script>
	import LoginUtil from '@/main/common/LoginUtil.js'
	import RouteUtil from '@/main/common/RouteUtil.js'
	export default {
		data() {
			return {
				loginType: '2',
				loginTitle: '手机号登录',
				loginTypeTitle: '账号密码登录',
				account: '17306060406',
				password: '',
				code: '708090',
			}
		},
		methods: {
			loginTypeChange() {
				if (1 == this.loginType) {
					this.loginType = 2;
					this.loginTitle = '手机号登录';
					this.loginTypeTitle = '账号密码登录';
				} else {
					this.loginType = 1;
					this.loginTitle = '密码登录';
					this.loginTypeTitle = '短信登录';
				}
			},
			goRegister() {
				RouteUtil.toRegister()
			},
			requestLogin() {
				if (!this.$refs.privacy.value()) {
					uni.$toast.showToast('请勾选隐私政策2222')
					return
				}
				let param = {
					loginName: this.account
				};
				if (1 == this.loginType) {
					param['passWord'] = this.password;
				} else {
					param['smsCode'] = this.code;
				}

				uni.$http.post('/api/login', param)
					.then(res => {
						LoginUtil.saveToken(res.data)
						uni.$http.get('/api/getLoginUser')
							.then(res => {
								LoginUtil.login(res)
							})
					})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.parent {
		background-color: #f5f5f5;
		height: 100vh;
		padding: 0 50rpx;

		.login-title {
			color: #222;
			font-size: 50rpx;
			font-weight: bold;
			margin-top: 260rpx;
		}

		.login-input {
			display: flex;
			flex-direction: row;
			align-items: center;
			background-color: #ededed;
			border-radius: 10rpx;
			height: 90rpx;
			margin-top: 50rpx;

			.label {
				width: 150rpx;
				height: 100%;
				line-height: 90rpx;
				font-size: 36rpx;
				color: #222;
				text-align: center;
				margin: auto 0;
			}

			.et {
				height: 100%;
				padding: 0 20rpx;
				font-size: 28rpx;
				flex: 1;
			}

		}

		.register {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.textButton {
				height: 50rpx;
				line-height: 50rpx;
				padding: 20rpx 20rpx;
				color: #666;
				font-size: 30rpx;
			}
		}
	}
</style>